import { _decorator, Component, Label, macro, Node, resources, Sprite, SpriteFrame, Tween, tween } from 'cc';
import { randomNum } from '../utils/utils';
import { UILayerMgr } from '../utils/UILayerMgr';
import { LayerBase } from '../utils/LayerBase';
import { UserInfo } from '../UserInfo';
import { Main } from './Main';
const { ccclass, property } = _decorator;

@ccclass('MatchPanel')
export class MatchPanel extends LayerBase {

    @property(Label)
    timeCountLabel: Label;
    @property(Label)
    selfNameLabel:Label;
    @property(Label)
    aiNameLabel:Label;
    @property(Sprite)
    sp:Sprite;
    @property(Sprite)
    aiSp:Sprite;

    // matchTimeCount;

    init() {
        // let scheduleTime=randomNum(1,4);
        // this.matchTimeCount=0;
        // this.schedule(() => {
        //     this.matchTimeCount+=1;
        //     this.timeCountLabel.string=this.matchTimeCount+"";
        // },1,scheduleTime,0.3)
        this.selfNameLabel.string = UserInfo.userName+"(我)";
        this.aiNameLabel.string = "???";

        resources.load(`avatar/${UserInfo.userAvatar}/spriteFrame`, SpriteFrame, (err, res) => {
            this.sp.spriteFrame = res;
        })

        this.aiSp.spriteFrame = null;


        let time = randomNum(1, 4);
        let obj = {
            n: 0
        }

        tween(obj)
            .tag(13)
            .to(time, { n: time }, {
                onUpdate: (target, ratio) => {
                    this.timeCountLabel.string = Math.round(obj.n) + ""
                }
            })
            .call(() => {
                let str = Main.ins.nameAllArr[randomNum(0, Main.ins.nameAllArr.length - 1)];
                let avatarIndex = randomNum(0, Main.ins.avatarTotalNum-1);
                this.aiNameLabel.string = str;
                resources.load(`avatar/${avatarIndex}/spriteFrame`, SpriteFrame, (err, res) => {
                    this.aiSp.spriteFrame = res;
                })

                setTimeout(() => {
                    UILayerMgr.getIns.closeUILayer(this.node.name);
                    UILayerMgr.getIns.openUILayer("matchGamePanel",{aiName:str,aiAvatarIndex:avatarIndex});
                }, 1500)
            })
            .start()
    }

    onclickCancelBtn() {
        Tween.stopAllByTag(13);
        UILayerMgr.getIns.closeUILayer(this.node.name);
    }


}


